<template>
  <main class="w-full h-full dark:bg-black p-4">
    <use-table
      :all-data="allData"
      :loading="loading"
      :columns="columns"
      v-model:selectedRowKey="selectedRowKey"
      :total="total"
      :search-select-option="searchSelectOption"
      :personalized="true"
      @delete-select-user="deleteJudge"
      @modify-user-data="openModifyJudgeForm"
    >
      <template #title> 判断题 </template>
      <template #function>
        <a-tooltip placement="bottom">
          <template #title>
            <span>添加题目</span>
          </template>
          <div class="cursor-pointer text-lg text-blue-500 mr-2" @click="openAddJudgeForm">
            <plus-circle-outlined />
          </div>
        </a-tooltip>
        <a-tooltip placement="bottom">
          <template #title>
            <span>删除选中题目</span>
          </template>
          <div class="cursor-pointer text-lg text-red-500" @click="deleteJudge">
            <close-circle-outlined />
          </div>
        </a-tooltip>
      </template>
    </use-table>
    <modify-form
      v-model:visible="addJudgeFormVisible"
      modal-title-text="添加判断题"
      @handleOk="completeAddJudgeForm"
    >
      <template #container>
        <div class="flex items-center">
          <span class="w-22 text-right"> 题目名称： </span>
          <a-input v-model:value="addJudgeFormData.judge_name" />
        </div>
        <div class="flex items-center mt-3">
          <span class="w-18 text-right"> 正确答案： </span>
          <a-radio-group v-model:value="addJudgeFormData.right_key">
            <a-radio :style="radioStyle" v-for="i in radioGroup" :value="i.value">
              {{ i.label }}
            </a-radio>
          </a-radio-group>
        </div>
        <div class="flex items-center mt-3">
          <span class="w-18 text-right"> 所属科目： </span>
          <a-select v-model:value="addJudgeFormData.subject_name" style="width: 180px">
            <a-select-option v-for="i in subjectList" :value="i.subject_name">
              {{ i.subject_name }}
            </a-select-option>
          </a-select>
        </div>
      </template>
    </modify-form>
    <modify-form
      v-model:visible="modifyJudgeFormVisible"
      modal-title-text="添加判断题"
      @handleOk="completeModifyJudgeForm"
    >
      <template #container>
        <div class="flex items-center">
          <span class="w-22 text-right"> 题目名称： </span>
          <a-input v-model:value="modifyJudgeFormData.judge_name" />
        </div>
        <div class="flex items-center mt-3">
          <span class="w-18 text-right"> 正确答案： </span>
          <a-radio-group v-model:value="modifyJudgeFormData.right_key">
            <a-radio :style="radioStyle" v-for="i in radioGroup" :value="i.value">
              {{ i.label }}
            </a-radio>
          </a-radio-group>
        </div>
        <div class="flex items-center mt-3">
          <span class="w-18 text-right"> 所属科目： </span>
          <a-select v-model:value="modifyJudgeFormData.subject_name" style="width: 180px">
            <a-select-option v-for="i in subjectList" :value="i.subject_name">
              {{ i.subject_name }}
            </a-select-option>
          </a-select>
        </div>
      </template>
    </modify-form>
  </main>
</template>

<script setup lang="ts">
  import { JudgeData, SubJect } from '@/utils/interface';
  import { getAllJudges, softDeleteJudge, addJudge, updateJudge } from '@/api/examination';
  import { getSubject } from '@/api/student';
  import { CloseCircleOutlined, PlusCircleOutlined } from '@ant-design/icons-vue';
  import { message } from 'ant-design-vue';
  // 搜索框下拉列表
  const searchSelectOption: { value: string; label: string }[] = [
    {
      value: 'subject_name',
      label: '专业名称',
    },
    {
      value: 'major_name',
      label: '科目名称',
    },
    {
      value: 'judge_name',
      label: '题目名称',
    },
  ];
  // 勾选的数据的key
  let selectedRowKey = ref<number[] | string[]>([]);
  const columns = [
    {
      title: '题目名称',
      dataIndex: 'judge_name',
      align: 'center',
      width: 360,
      ellipsis: true,
    },
    {
      title: '正确答案',
      dataIndex: 'show_key',
      align: 'center',
      width: 80,
      ellipsis: true,
    },
    {
      title: '所属科目',
      dataIndex: 'subject_name',
      align: 'center',
      ellipsis: true,
    },
    {
      title: '所属专业',
      dataIndex: 'major_name',
      align: 'center',
      ellipsis: true,
    },
    {
      title: '添加时间',
      dataIndex: 'add_time',
      align: 'center',
      ellipsis: true,
      sorter: (a, b) => a.add_time - b.add_time,
    },
    {
      title: '操作',
      dataIndex: 'operation',
      align: 'center',
      width: 140,
    },
  ];
  let allData = ref<JudgeData[]>([]);
  let loading = ref(false);
  let total = ref(0);
  const getJudgesList = async () => {
    loading.value = true;
    const { data } = await getAllJudges();
    allData.value = data.filter((item) => item.status !== 0);
    allData.value.forEach((item) => {
      item.show_key = Number(item.right_key) === 0 ? '错误' : '正确';
    });
    total.value = allData.value.length;
    loading.value = false;
  };
  let subjectList = ref<SubJect[]>([]);
  getJudgesList();
  getSubject().then(({ data }) => {
    subjectList.value = data as SubJect[];
  });

  const deleteJudge = async (key: number[] | any) => {
    const selectKey = Array.isArray(key) ? key : selectedRowKey.value;
    if (selectKey.length === 0) {
      message.warning('请选择要删除的题目');
      return;
    }
    const res = (await softDeleteJudge(selectKey)) as any;
    if (res.status === 200) {
      message.success(res.msg);
      getJudgesList();
    }
  };

  const radioGroup = [
    { value: 0, label: '错误' },
    { value: 1, label: '正确' },
  ];
  const radioStyle = {
    display: 'flex',
    height: '30px',
    lineHeight: '30px',
  };

  let addJudgeFormVisible = ref(false);
  let addJudgeFormData = ref({
    judge_name: '',
    right_key: '0',
    subject_name: '',
  });
  const openAddJudgeForm = () => {
    addJudgeFormVisible.value = true;
  };
  const completeAddJudgeForm = async () => {
    const { judge_name, right_key, subject_name } = addJudgeFormData.value;
    if (!judge_name || !subject_name) {
      message.warning('请输入完整内容');
      return;
    }
    const { status, msg } = (await addJudge({ judge_name, right_key, subject_name })) as any;
    if (status === 200) {
      message.success(msg);
      await getJudgesList();
      addJudgeFormData.value = {
        judge_name: '',
        right_key: '0',
        subject_name: '',
      };
      addJudgeFormVisible.value = false;
    }
  };

  let modifyJudgeFormVisible = ref(false);
  let modifyJudgeFormData = ref<JudgeData>({
    judge_name: '',
    right_key: '0',
    subject_name: '',
    key: 0,
  });
  const openModifyJudgeForm = (key: number) => {
    const clickUser = allData.value.find((item: any) => item.key === key) as any;
    if (clickUser !== undefined) {
      modifyJudgeFormData.value = {
        ...clickUser,
      };
      modifyJudgeFormVisible.value = true;
    } else message.error('发生错误');
  };
  const completeModifyJudgeForm = async () => {
    const { judge_name, right_key, subject_name } = modifyJudgeFormData.value;
    if (!judge_name || !subject_name) {
      message.warning('请输入完整内容');
      return;
    }
    const { status, msg } = (await updateJudge({
      id: modifyJudgeFormData.value.key,
      judge_name,
      right_key,
      subject_name,
    })) as any;
    if (status === 200) {
      message.success(msg);
      await getJudgesList();
      modifyJudgeFormVisible.value = false;
    }
  };
</script>

<style scoped>
  :deep(.ant-radio-group-default) {
    @apply flex;
  }
</style>
